<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>常量表达式的枚举值</title>
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-base.css" />
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-medium.css" />

 </head>
 <body class="docs"><div class="navbar navbar-fixed-top">
  <div class="navbar-inner clearfix">
    <ul class="nav" style="width: 100%">
      <li style="float: left;"><a href="language.enumerations.traits.html">« Trait</a></li>
      <li style="float: right;"><a href="language.enumerations.object-differences.html">和对象的差异 »</a></li>
    </ul>
  </div>
</div>
<div id="breadcrumbs" class="clearfix">
  <ul class="breadcrumbs-container">
    <li><a href="index.html">PHP Manual</a></li>
    <li><a href="language.enumerations.html">枚举</a></li>
    <li>常量表达式的枚举值</li>
  </ul>
</div>
<div id="layout">
  <div id="layout-content"><div id="language.enumerations.expressions" class="sect1">
  <h2 class="title">常量表达式的枚举值</h2>

  <p class="para">
    由于用 enum 自身的常量表示条目，它们可当作静态值，用于绝大多数常量表达式：
    属性默认值、变量默认值、参数默认值、全局和类常量。
    他们不能用于其他 enum 枚举值，但通常的常量可以引用枚举条目。
  </p>

  <p class="para">
   然而，因为不能保证结果值绝对不变，也不能避免调用方法时带来副作用，
   所以枚举里类似 <span class="classname"><a href="class.arrayaccess.html" class="classname">ArrayAccess</a></span> 这样的隐式魔术方法调用无法用于静态定义和常量定义。
   常量表达式还是不能使用函数调用、方法调用、属性访问。
  </p>

  <div class="example-contents">
   <div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">//&nbsp;这是完全合法的&nbsp;Enum&nbsp;定义<br /></span><span style="color: #0000BB">enum&nbsp;Direction&nbsp;</span><span style="color: #007700">implements&nbsp;</span><span style="color: #0000BB">ArrayAccess<br /></span><span style="color: #007700">{<br />&nbsp;&nbsp;&nbsp;&nbsp;case&nbsp;</span><span style="color: #0000BB">Up</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;case&nbsp;</span><span style="color: #0000BB">Down</span><span style="color: #007700">;<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;function&nbsp;</span><span style="color: #0000BB">offsetGet</span><span style="color: #007700">(</span><span style="color: #0000BB">$val</span><span style="color: #007700">)&nbsp;{&nbsp;...&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;function&nbsp;</span><span style="color: #0000BB">offsetExists</span><span style="color: #007700">(</span><span style="color: #0000BB">$val</span><span style="color: #007700">)&nbsp;{&nbsp;...&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;function&nbsp;</span><span style="color: #0000BB">offsetSet</span><span style="color: #007700">(</span><span style="color: #0000BB">$val</span><span style="color: #007700">)&nbsp;{&nbsp;throw&nbsp;new&nbsp;</span><span style="color: #0000BB">Exception</span><span style="color: #007700">();&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;function&nbsp;</span><span style="color: #0000BB">offsetUnset</span><span style="color: #007700">(</span><span style="color: #0000BB">$val</span><span style="color: #007700">)&nbsp;{&nbsp;throw&nbsp;new&nbsp;</span><span style="color: #0000BB">Exception</span><span style="color: #007700">();&nbsp;}<br />}<br /><br />class&nbsp;</span><span style="color: #0000BB">Foo<br /></span><span style="color: #007700">{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;可以这样写。<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">const&nbsp;</span><span style="color: #0000BB">Bar&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">Direction</span><span style="color: #007700">::</span><span style="color: #0000BB">Down</span><span style="color: #007700">;<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;由于它是不确定的，所以不能这么写。<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">const&nbsp;</span><span style="color: #0000BB">Bar&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">Direction</span><span style="color: #007700">::</span><span style="color: #0000BB">Up</span><span style="color: #007700">[</span><span style="color: #DD0000">'short'</span><span style="color: #007700">];<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;Fatal&nbsp;error:&nbsp;Cannot&nbsp;use&nbsp;[]&nbsp;on&nbsp;enums&nbsp;in&nbsp;constant&nbsp;expression<br /></span><span style="color: #007700">}<br /><br /></span><span style="color: #FF8000">//&nbsp;由于它不是一个常量表达式，所以是完全合法的<br /></span><span style="color: #0000BB">$x&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">Direction</span><span style="color: #007700">::</span><span style="color: #0000BB">Up</span><span style="color: #007700">[</span><span style="color: #DD0000">'short'</span><span style="color: #007700">];<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
  </div>

 </div></div></div></body></html>